package crud.service;

import crud.dao.EmployeeMapper;
import crud.pojo.Employee;
import crud.pojo.EmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class employeeServiceImpl implements employeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<Employee> getAll() {
        //null表示无条件查询所有信息。
        List<Employee> employees = employeeMapper.selectByExampleWithDept(null);
        return employees;
    }

    @Override
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }
    /**
     * 检验用户名是否可用
     * @param empName
     * @return true代表姓名可用，false代表不可用
     */
    @Override
    public boolean checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        //        如果count=0，返回true
        return count==0;
    }

    @Override
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public List<Employee> getEmpByVague(String content) {
        System.out.println("service查询功能");
        List<Employee> employees = employeeMapper.selectByVague(content);
        return employees;
    }

    @Override
    public void deleteBatch(List<Integer> del_ids) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpIdIn(del_ids);
        employeeMapper.deleteByExample(employeeExample);
    }

    @Override
    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }
}
